package com.jiuyuejiu.func_video.adapter;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.jiuyuejiu.R;
import com.jiuyuejiu.func_video.domain.VideoDetailStc;
import com.jiuyuejiu.func_video.domain.VideoFenleiStc;
import com.jiuyuejiu.utils.ShanHaiUtil;

import java.util.LinkedHashMap;
import java.util.List;

import cn.jzvd.JZDataSource;
import cn.jzvd.JzvdStd;

/**
 * Created by y
 * On 2016/02/07 01:20
 */
public class VideoListAdapter extends BaseAdapter {

    public static final String TAG = "VideoListAdapter";

    private static final RequestOptions BANNER_OPTIONS = new RequestOptions()
            .diskCacheStrategy(DiskCacheStrategy.ALL)// 缓存SOURC和RESULT
            .dontAnimate()// 移除所有的动画
            // .fitCenter()// 该api可能 铺不满整个ImageView控件
            .centerCrop()// 按比例放大/缩小,铺满整个ImageView控件
            .placeholder(R.drawable.shape_solid_cccccc)// 占位图
            //.transform(new CenterCrop(), new RoundedCorners(18))
            ;

    private Context context;
    private VideoFenleiStc videoFenleiStc;
    private List<VideoDetailStc> videoDetailStcs;

    // 宽高参数
    private LinearLayout.LayoutParams linearParams;

    public VideoListAdapter(Context context, VideoFenleiStc stc, List<VideoDetailStc> videoDetailStcs) {
        this.context = context;
        this.videoFenleiStc = stc;
        this.videoDetailStcs = videoDetailStcs;


        // ----↓ 图片高度 2022/6/4 17:46 ↓------------------------------------------------------------------------------------
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        int screenWidth = (int) (dm.widthPixels );
        int screenHeight = (int) (screenWidth / 2);
        linearParams = new LinearLayout.LayoutParams(screenWidth, screenHeight);
        // ----↑ 图片高度 2022/6/4 17:46 ↑------------------------------------------------------------------------------------
    }

    @Override
    public int getCount() {
        return videoDetailStcs.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (null == convertView) {
            viewHolder = new ViewHolder();
            LayoutInflater mInflater = LayoutInflater.from(context);
            convertView = mInflater.inflate(R.layout.item_video_view, null);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        VideoDetailStc item = (VideoDetailStc) videoDetailStcs.get(position);
        viewHolder.jzvdStd = convertView.findViewById(R.id.videoplayer);
        // 设置宽高
        viewHolder.jzvdStd.setLayoutParams(linearParams);
        //
        LinkedHashMap map = new LinkedHashMap();
        String videourl = ShanHaiUtil.getHttpid() + item.getVideopath() + item.getVideoname();

        // map.put("普清", item.getVideourl());
        map.put("高清", videourl);
        map.put("标清", videourl);
        map.put("普清",videourl);
        JZDataSource jzDataSource = new JZDataSource(map, item.getVideoname());
        jzDataSource.looping = true;
        jzDataSource.currentUrlIndex = 2;
        jzDataSource.headerMap.put("key", "value");//header
        viewHolder.jzvdStd.setUp(jzDataSource
                , JzvdStd.SCREEN_NORMAL);

        /*viewHolder.jzvdStd.setUp(
                item.getVideourl(),
                item.getVideoname(), Jzvd.SCREEN_NORMAL);*/

        viewHolder.jzvdStd.titleTextView.setTextSize(12);
        //
        Glide.with(convertView.getContext())
                .load(item.getImageurl())
                .apply(BANNER_OPTIONS)
                .into(viewHolder.jzvdStd.posterImageView);

        viewHolder.jzvdStd.positionInList = position;
        return convertView;
    }

    class ViewHolder {
        JzvdStd jzvdStd;
    }
}
